package com.huisu;

import java.util.ArrayList;
import java.util.List;

public class Test01 {
    public List<List<Integer>> permute(int[] nums) {
        int len = nums.length;
        // 使用一个动态数组保存所有可能的全排列
        List<List<Integer>> res = new ArrayList<>();
        if (len == 0) {
            return res;
        }

        boolean[] used = new boolean[len];
        List<Integer> path = new ArrayList<>();

        dfs(nums, len, 0, path, used, res);
        return res;
    }


    public void dfs(int[] nums, int len, int depth, List<Integer> path,boolean[] used,List<List<Integer>> res){
        if (depth==len){
            res.add(new ArrayList<>(path));
            return;
        }
        for (int i = 0; i < len; i++) {

            if (!used[i]){
                path.add(nums[i]);
                used[i]=true;
                dfs(nums,len,depth+1,path,used,res);
                used[i]=false;
                path.remove(path.size()-1);
            }
        }
    }

}
